import Vue from 'vue'
import VueRouter from 'vue-router'

import login from '../view/login/index.vue'

// 导入底部
import index from '../home/index.vue'
import movie from '../home/movie.vue'
import my from '../home/my.vue'
import question from '../home/question.vue'
// 编辑跳转
import edit from '../view/layout/edit/index.vue'
// 路由重定向和404
import notfound from '../view/notfound/index.vue'
// 导入重新登录
import { getToken, removeToken } from '../until/token.js'
import { Toast } from 'vant'
// 导入信息接口
import { getUserInfo } from '../api/user.js'
// import { editUserInfo } from '../api/edit.js'
// 仓库对象
import store from '../store/index.js'
import articleList from '../home/articleList.vue'
import article from '../view/layout/article/index.vue'
import search from '../view/layout/search/index.vue'
import result from '../view/layout/result/index.vue'
// import comments from '../view/layout/article/comment.vue'
import student from '../view/student/index.vue'
Vue.use(VueRouter)
const routes = [
  {
    path: '/login',
    component: login
  },
  {
    path: '/index',
    component: index,
    meta: {
      showTab: true // 添加元信息
    }
  },
  {
    path: '/movie',
    component: movie,
    meta: {
      showTab: true // 添加元信息
    }
  },
  {
    path: '/my',
    component: my,
    meta: {
      showTab: true, // 添加元信息
      needLogin: true // 标记需要登录
    }
  },
  {
    path: '/question',
    component: question,
    meta: {
      showTab: true // 添加元信息
    }
  },
  {
    path: '/notfound',
    component: notfound // 配置规则
    // meta: {
    //   showTab: true // 添加元信息
    // }
  },
  {
    path: '/edit',
    component: edit,
    meta: {
      // showTab: true, // 添加元信息
      needLogin: true
    }
  },
  {
    path: '/articleList',
    component: articleList,
    meta: {
      needLogin: true
    }
  },
  {
    path: '/article/:id',
    name: 'article',
    component: article
  },
  {
    path: '/search',
    component: search,
    meta: {
      needLogin: true
    }
  },
  {
    path: '/result',
    component: result,
    meta: {
      needLogin: true
    }
  },
  // {
  //   path: '/comments',
  //   component: comments,
  //   meta: {
  //     needLogin: true
  //   }
  // },
  {
    path: '/student',
    component: student,
    meta: {
      // showTab: true, // 添加元信息
      needLogin: true
    }
  },
  {
    path: '/',
    redirect: '/my' // 配置重定向
  },
  {
    path: '*',
    redirect: '/notfound'
  }
]

// 处理路由切换的Promise的reject错误
// 吞掉 没有处理的 promise的错误
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) {
    // 执行之后 阻断后续代码
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes
})
// 添加导航守卫，全局前置守卫 切换之前执行
router.beforeEach((to, from, next) => {
  console.log('from:', from)
  // 判断是否需要登录，通过meta来判断
  if (to.meta.needLogin !== true) {
    // 直接访问
    return next()
  }
  // 到这，需要登录才可以访问
  const tokenObj = getToken()
  console.log('tokenObj:', tokenObj)
  if (tokenObj === null) {
    Toast.fail('请先登录')
    // 去登陆
    return next({ path: '/login', query: { back: to.path } })
  }
  if (store.state.userInfo.name) {
    // 用户信息存在
    return next()
  }
  // token 不为空，判断对错
  getUserInfo()
    .then(res => {
      // console.log('res:', res)
      store.commit('setUserInfo', res.data.data)
      next()
    })
    .catch(errRes => {
      if (errRes.response.status === 401) {
        removeToken()
        Toast.fail('请先登录')
        next({ path: '/login', query: { back: to.path } })
      }
    })
})

export default router
